package com.lzb.newloan;


import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.lzb.newloan.utils.AlertDialog;
import com.lzb.newloan.utils.Config;
import com.lzb.newloan.utils.HttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import static com.lzb.newloan.R.id.feedback_linear;
import static com.lzb.newloan.R.id.problem_linear;

public class InvestMoreFragment extends Fragment implements OnClickListener {
    private View view;
    private Activity ac;
    private int rzstatus;
    private android.app.AlertDialog phoneDialog;
    private String userId;
    private TextView invester;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_more, container, false);
            initView();
//			initData();
        } else {
            ((ViewGroup) view.getParent()).removeView(view);
        }
        return view;
    }

    private void initView() {
        ac = getActivity();
        SharedPreferences sp = ac.getSharedPreferences("config", 0x0000);
        String phone = sp.getString("phone", "");
        userId = sp.getString("userid", "");
        rzstatus = sp.getInt("rzstatus", 0);

        view.findViewById(R.id.applay_linear).setOnClickListener(this);
        view.findViewById(R.id.about_us_linear).setOnClickListener(this);
        view.findViewById(feedback_linear).setOnClickListener(this);
        view.findViewById(R.id.contact_linear).setOnClickListener(this);
        view.findViewById(problem_linear).setOnClickListener(this);
        invester = (TextView) view.findViewById(R.id.invester);
        invester.setText("切换到贷款");
//        HttpUtils.doPostAsyn(Config.HOME_INIT + "&userid=" + userId, "", mHandler, Config.CODE_HOME_INIT);
    }


    private Handler mHandler = new Handler() {

        public void handleMessage(android.os.Message msg) {

            switch (msg.what) {

                case Config.CODE_HOME_INIT:
                    try {
                        JSONObject response = new JSONObject(msg.obj.toString());
                        rzstatus = Integer.parseInt(response
                                .getString("rzstatus"));
                        int invest_status = Integer.parseInt(response
                                .getString("invest_status"));
                        if (rzstatus == 1) {
                            if (invest_status == 1) {
                                invester.setText("切换到投资");
                            } else {
                                invester.setText("申请投资人");
                            }

                        } else {
                            invester.setText("申请投资人");
                        }

                    } catch (JSONException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (NumberFormatException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    break;

                case Config.CODE_URL_ERROR:
                    Toast.makeText(ac, "url错误",
                            Toast.LENGTH_SHORT).show();
                    break;
                case Config.CODE_NET_ERROR:
                    Toast.makeText(ac, "网络错误",
                            Toast.LENGTH_SHORT).show();
                    // if (time!=null) {
                    // time.cancel();
                    // }
                    break;

                default:
                    break;
            }
        }

    };

    @Override
    public void onClick(View v) {
        ConnectionDetector cd = new ConnectionDetector(ac);
        switch (v.getId()) {
            case R.id.applay_linear:
                startActivity(new Intent(ac,IndexActivity.class));

                break;
            case R.id.about_us_linear:
                if (cd.isConnectingToInternet()) {

                    Intent intent2 = new Intent(ac, AboutUsActivity.class);
                    startActivity(intent2);
                }
                break;
            case feedback_linear:
                if (cd.isConnectingToInternet()) {
                    Intent intent3 = new Intent(ac, CommitAdviseActivity.class);
                    intent3.putExtra("type", "person");
                    startActivity(intent3);
                }
                break;
            case R.id.contact_linear:
                if (cd.isConnectingToInternet()) {
                    this.phoneDialog = new android.app.AlertDialog.Builder(ac)
                            .create();
                    LayoutInflater lay = LayoutInflater
                            .from(((Context) ac));
                    final View inflate = lay.inflate(R.layout.layout_dialog2,
                            (ViewGroup) null);
                    inflate.findViewById(R.id.cancle).setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            phoneDialog.dismiss();
                        }
                    });

                    final TextView textView = (TextView) inflate
                            .findViewById(R.id.dialog_ok);
                    ((TextView) inflate.findViewById(R.id.dialog_cancle))
                            .setOnClickListener((OnClickListener) new OnClickListener() {
                                public void onClick(final View view) {
                                    phoneDialog.dismiss();
                                }
                            });
                    inflate.findViewById(R.id.wx_problem).setOnClickListener(
                            new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    // 打开微信
                                    phoneDialog.dismiss();
                                    try {
                                        Intent intent = new Intent(Intent.ACTION_MAIN);
                                        ComponentName cmp = new ComponentName(
                                                "com.tencent.mm",
                                                "com.tencent.mm.ui.LauncherUI");
                                        intent.addCategory(Intent.CATEGORY_LAUNCHER);
                                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                        intent.setComponent(cmp);
                                        startActivity(intent);
                                    } catch (ActivityNotFoundException e) {
                                        Toast.makeText(ac,
                                                "检查到您手机没有安装微信，请安装后使用该功能",
                                                Toast.LENGTH_LONG).show();
                                    }

                                }
                            });
                    inflate.findViewById(R.id.qq_problem).setOnClickListener(
                            new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    // 打开扣扣进行客服沟通
                                    phoneDialog.dismiss();
                                    try {
                                        String url = "mqqwpa://im/chat?chat_type=wpa&uin=3387316533";
                                        startActivity(new Intent(Intent.ACTION_VIEW,
                                                Uri.parse(url)));
                                    } catch (ActivityNotFoundException e) {
                                        Toast.makeText(ac,
                                                "检查到您手机没有安装QQ，请安装后使用该功能",
                                                Toast.LENGTH_LONG).show();
                                    }
                                }
                            });
                    inflate.findViewById(R.id.tel_problem).setOnClickListener(
                            new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    // 拨打电话
                                    phoneDialog.dismiss();
                                    startActivity(new Intent(
                                            Intent.ACTION_DIAL, Uri
                                            .parse("tel:4006899830")));

                                }
                            });
                    textView.setOnClickListener((OnClickListener) new OnClickListener() {
                        public void onClick(final View view) {
                        }
                    });
                    this.phoneDialog.show();
                    this.phoneDialog.setCanceledOnTouchOutside(false);
                    this.phoneDialog.setContentView(inflate);
                }
                break;
            case problem_linear:
                if (cd.isConnectingToInternet()) {
                    Intent intent = new Intent(ac, ProblemActivity.class);
                    intent.putExtra("title", "常见问题");
                    intent.putExtra("url", Config.COMMON_PROBLEM);
                    startActivity(intent);
                }
                break;
        }
    }
}
